<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 头部按钮 -->
        <div class="headbox">
          <el-row>
            <el-col :span="12"><span class="headSt">说明：目前支持学科和关键字条件筛选</span></el-col>
            <el-col :span="12" style="text-align:right"
              ><el-button size="small" type="success" @click="toQuestionsLR"
                ><i class="el-icon-edit"></i>新增试题</el-button
              ></el-col
            >
          </el-row>
        </div>
        <!-- 内容主题 form表单部分 -->
        <el-form ref="form" :model="form" label-width="80px" size="small" class="formstyle">
          <el-col :span="6"
            ><el-form-item label="学科">
              <el-select v-model="form.subject" placeholder="请选择" @change="toCopy">
                <el-option
                  v-for="(item, index) in formDetail"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="6"
            ><el-form-item label="二级目录">
              <el-select v-model="form.catalogID" placeholder="请选择">
                <el-option
                  v-for="(item, index) in formMulu"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="6"
            ><el-form-item label="标签">
              <el-select v-model="form.tags" placeholder="请选择">
                <el-option
                  v-for="(item, index) in formBiaoQ"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="6"
            ><el-form-item label="关键字">
              <el-input placeholder="根据题干搜索" v-model="form.keyword"></el-input> </el-form-item
          ></el-col>
          <el-col :span="6"
            ><el-form-item label="试题类型">
              <el-select v-model="form.questionType" placeholder="请选择">
                <el-option
                  v-for="(item, index) in questionType"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="6"
            ><el-form-item label="难度">
              <el-select v-model="form.difficulty" placeholder="请选择">
                <el-option
                  v-for="(item, index) in difficulty"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="6"
            ><el-form-item label="方向">
              <el-select v-model="form.direction" placeholder="请选择">
                <el-option v-for="(item, index) in direction" :key="index" :label="item" :value="index"></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="6"
            ><el-form-item label="录入人">
              <el-select v-model="form.creator" placeholder="请选择">
                <el-option label="" value="超级管理员"></el-option>
                <el-option label="" value="录入管理员"></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="6"
            ><el-form-item label="题目备注"> <el-input v-model="form.rty"></el-input> </el-form-item
          ></el-col>
          <el-col :span="6"
            ><el-form-item label="企业简称"> <el-input v-model="form.dfg"></el-input> </el-form-item
          ></el-col>
          <el-col :span="6"
            ><el-form-item label="城市">
              <!-- 城市city -->
              <mycity :value="form" @input="input"></mycity> </el-form-item
          ></el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button size="small" @click="clearSelect">清除</el-button>
              <el-button type="primary" size="small" @click="searchMessage(form.keyword, form.subject)">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-form>
        <!-- 内容主题 tabel表格部分 -->
        <el-alert :title="`数据一共${total}条`" type="info" :closable="false" show-icon></el-alert>
        <el-table class="tablesty" :data="tableData" stripe style="width: 100%">
          <el-table-column prop="number" label="试题编号" width="180"> </el-table-column>
          <el-table-column prop="subject" label="学科" width="180"> </el-table-column>
          <el-table-column prop="catalog" label="目录"> </el-table-column>
          <el-table-column label="题型">
            <template slot-scope="{ row }">{{ row.questionType | ctitle }}</template>
          </el-table-column>
          <el-table-column label="题干">
            <template slot-scope="{ row }">
              {{ row.question | html2Text }}
            </template></el-table-column
          >

          <el-table-column label="录入时间"
            ><template slot-scope="{ row }">{{ row.addDate | parseTimeByString }}</template></el-table-column
          >
          <el-table-column prop="difficulty" label="难度">
            <template slot-scope="{ row }">{{ row.difficulty | questionDiffer }}</template>
          </el-table-column>
          <el-table-column prop="creator" label="录入人"> </el-table-column>
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="{ row }">
              <el-button icon="el-icon-search" @click="showDialog(row.id)" circle></el-button>
              <el-button type="primary" icon="el-icon-edit" @click="toQuestionsById(row.id)" circle></el-button>
              <el-button type="success" icon="el-icon-check" @click="toCullingQuestions(row.id)" circle></el-button>
              <el-button type="danger" icon="el-icon-delete" @click="delQuestionsById(row.id)" circle></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 底部 分页部分 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pagesizes"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
        >
        </el-pagination>
        <!-- 试题预览部分 -->
        <mydialog ref="mtdialog" :dialogForm.sync="dialogForm"></mydialog>
      </el-card>
    </div>
  </div>
</template>
<script>
// import { profile } from '@/api/base/frame.js'
import { simple as simple2 } from '@/api/hmmm/tags.js'
import { simple as simples } from '@/api/hmmm/directorys.js'
import { difficulty, questionType, direction } from '@/api/hmmm/constants.js'
import { list, remove, choiceAdd } from '@/api/hmmm/questions.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  components: {
    mydialog: () => import('@/module-hmmm/components/questions-views.vue'),
    mycity: () => import('@/module-hmmm/pages/companys/citys.vue')
  },
  data () {
    return {
      // 试题预览弹框
      dialogForm: false,
      // 表单难度列表数据
      difficulty: difficulty,
      // 表单题型列表数据
      questionType: questionType,
      // 表单方向列表数据
      direction: direction,
      // 表单数据
      form: {
        subject: '',
        catalogID: '',
        tags: '',
        difficulty: '',
        province: '',
        city: '',
        keyword: '',
        questionType: ''
      },
      // form表格信息列表（学科）
      formDetail: [],
      // form表格信息列表（目录）
      formMulu: [],
      // form表格信息列表（标签）
      formBiaoQ: [],

      // table表格数据
      tableData: [],
      // 分页数据
      currentPage: 1, // 当前页
      pagesize: 5, // 当前页容量
      total: 0, // 总计
      pagesizes: [5, 10, 20, 50] // 每页显示几条
    }
  },
  created () {
    // tabel表格信息列表
    this.getQuestionList()
    // form表格信息列表（学科）
    this.geuSubjectList()
  },
  methods: {
    // 页容量发生改变触发
    handleSizeChange (size) {
      this.pagesize = size
      this.getQuestionList()
    },
    // 页码值发生改变触发
    handleCurrentChange (page) {
      this.currentPage = page
      this.getQuestionList()
    },
    // 获取tabel表格要的数据
    async getQuestionList () {
      const res = await list({ page: this.currentPage, pagesize: this.pagesize })
      this.tableData = res.data.items
      this.total = res.data.counts
    },

    // 点击预览试题展示弹框 并发起请求获取到试题详情
    showDialog (id) {
      this.dialogForm = true
      this.$refs.mtdialog.getdialogForm(id)
    },
    // 点击新增试题跳转到试题录入
    toQuestionsLR () {
      this.$router.push('/questions/new')
    },
    // 点击tabel表格中的试题录入按钮通过id跳转
    toQuestionsById (id) {
      this.$router.push(`/questions/new?id=${id}`)
    },
    // 点击删除 询问 发请求
    async delQuestionsById (id) {
      const result = await this.$confirm('此操作将删除该题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      if (result !== 'confirm') return
      await remove({ id })
      this.$message.success('删除成功')
      this.getQuestionList()
    },
    // 点击加入精选并跳转页面
    async toCullingQuestions (id) {
      const result = await this.$confirm('此操作会将该题加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      if (result !== 'confirm') return
      const res = await choiceAdd({ id: id, choiceState: 1 })
      console.log(res)
      this.$message.success('加入精选题库成功')
      this.$router.push(`/questions/choice?${id}`)
      this.getQuestionList()
    },
    // 获取学科数据完成学科下拉框列表渲染
    async geuSubjectList () {
      const res = await simple()
      // console.log(res)
      this.formDetail = res.data
    },
    // 当下拉选项发生改变时将选中的值赋值给form.subject 当选择框有值的时候就可以获取对应的二级目录列表并渲染了
    async toCopy (id, value) {
      // 得到当前学科的id传值给目录简单列表 然后得到数据给form表单的目录数组赋值完成渲染
      if (value !== '') {
        const res1 = await simples({ subjectID: id })
        this.formMulu = res1.data
        // 下面时标签列表
        const res2 = await simple2({ subjectID: id })
        this.formBiaoQ = res2.data
      }
    },
    // 根据题干关键字搜索相关信息并刷新列表数据
    async searchMessage (keyword, value) {
      if (keyword) {
        const res = await list({ keyword: keyword })
        this.tableData = res.data.items
      } else if (value) {
        const res = await list({ subjectID: value })
        this.tableData = res.data.items
      }
    },
    // 城市的下拉选项框
    input (val) {
      this.form.province = val.province
      this.form.city = val.city
    },
    // 点击清除刷新列表
    clearSelect () {
      this.form = {}
      this.getQuestionList()
    }
  }
}
</script>

<style lang="scss" scoped>
.headSt {
  color: #ffc0cb;
  font-size: 13px;
}
.headbox {
  margin-bottom: 20px;
}
.el-form-item :last-child {
  text-align: right;
}
.el-input {
  width: 205px;
}
.city {
  width: 90px;
}
.formstyle {
  margin-bottom: 20px;
}
.el-alert {
  margin-bottom: 20px;
}
.tablesty {
  margin-bottom: 20px;
}
.el-pagination {
  text-align: right;
}
</style>
